import { CurrencyPipe } from '@angular/common';
import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';

import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-transaction-flow',
  templateUrl: './transaction-flow.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class TransactionFlowComponent extends BasicTableComponent {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  constructor(public service: FreightAccountService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }
  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        }
      });
    }
    return requestOptions;
  };


  private initSF(): SFSchema {
    return {
      properties: {
        createTime: {
          title: '交易时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择'
          } as SFDateWidgetSchema
        },
        transactionNumber: {
          type: 'string',
          title: '流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        businessNumber: {
          type: 'string',
          title: '关联单号',
          ui: {
            placeholder: '请输入'
          }
        },
        orderSn: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入',
          }
        },
        tradeType: {
          type: 'string',
          title: '交易类型',
          enum: [
            { label: '全部', value: '' },
            { label: '运费支付', value: 1 },
            { label: '附加费支付', value: 2 },
            { label: '运费退款', value: 3 },
            { label: '附加费退款', value: 4 },
            { label: '保费支付', value: 5 },
            { label: '保费退款', value: 6 },
            { label: '余额充值', value: 7 },
            { label: '余额提现', value: 8 },
            { label: '资金分配', value: 9 },
            { label: '资金回收', value: 10 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
          },
          default: ''
        },
        incomeType: {
          type: 'string',
          title: '收支类型',
          enum: [
            { label: '全部', value: '' },
            { label: '收入', value: 2 },
            { label: '支出', value: 1 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
          },
          default: ''
        },
        channelSource: {
          type: 'string',
          title: '账户类型',
          enum: [
            { label: '全部', value: '' },
            { label: '货主账户', value: '1' },
            { label: '司机账户', value: '2' },
            { label: '运营商账户', value: '3' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
          },
          default: ''
        },
        roleName: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
          }
        },
        projectId: {
          type: 'string',
          title: '所属项目',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getEnterpriseProject()
          },
          default: ''
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
          },
          default: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '交易时间', index: 'createTime', width: 180 },
      { title: '流水号', index: 'transactionNumber', width: 180 },
      { title: '交易类型', index: 'tradeTypeLabel', width: 120 },
      { title: '关联单号', index: 'businessNumber', width: 170 },
      { title: '订单号', index: 'orderSn', width: 170 },
      { title: '账户类型', index: 'accountTypeLabel', width: 130 },
      { title: '账户名称', index: 'roleName', width: 180 },
      { title: '所属项目', index: 'projectName', width: 140 },
      { title: '收支类型', index: 'incomeTypeLabel', width: 100, className: 'text-center' },
      {
        title: '交易金额',
        index: 'amount',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.amount }) }
      },
      {
        title: '账户余额',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.accountBalance }) }
      },
      { title: '网络货运人', index: 'ltdName', width: 180 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '银行流水号', index: 'channelPaySn', width: 170 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '查看回单',
            iif: item => item.tradeType !== '9' && item.tradeType !== '10',
            acl: { ability: ['FINANCIAL-TRANSACTION-receiptApply'] },
            click: item =>
              this.service.getReceiptUrl(item.receiptUrl, {
                bankType: item.bankType,
                rmYll: item.roleId,
                snglFlgCd: item.channelPaySn,
                bussType: item.tradeType === '7' ? '05' : item.tradeType === '8' ? '06' : '07',
                ltdId: item.ltdId,
                accountType: item.accountType
              })
          }
        ]
      }
    ];
  }
  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_exportAccountBalanceDetailPage);
  }
}
